import { loginApi, codeApi } from '@/api/user'
import { Message } from 'element-ui'
const state = {
  token: null,
  imgSrc: '',
  userInfo: {}
}

const mutations = {
  setToken(state, val) {
    state.token = val
  },
  removeToken(state) {
    state.token = null
  },
  setUserInfo(state, val) {
    state.userInfo = val
  },
  removeUserInfo(state) {
    state.userInfo = null
  }
}

const actions = {
  async loginAction({ commit }, data) {
    const res = await loginApi(data)
    console.log(res)
    if (res.success) {
      Message.success(res.msg)
      commit('setToken', res.token)
      commit('setUserInfo', res)
    } else {
      Message.error(res.msg)
    }
  },
  async codeAction({ state }, num) {
    const res = await codeApi(num)
    state.imgSrc = window.URL.createObjectURL(res)
  },
  logOutAction({ commit }) {
    commit('removeToken')
    commit('removeUserInfo')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

